import hashlib
import json
import random
import string
import time
import uuid
from typing import Iterable, List, Union

from forward.config import get_settings

ALL_RANDOM_STR: str = string.ascii_letters + string.digits


class ToolsUtil:
    """常用工具集合类"""

    secret: str = get_settings().secret

    @staticmethod
    def random_string(length: int) -> str:
        """返回随机字符串"""
        return "".join(random.choices(ALL_RANDOM_STR, k=length))

    @staticmethod
    def random_int(length: int) -> int:
        """
        返回指定长度的随机整数
        :param length:
        :return:
        """
        return random.randint(0, 10**length)

    @staticmethod
    def make_uuid() -> str:
        """制作UUID"""
        return uuid.uuid4().hex

    @staticmethod
    def make_md5(data: str) -> str:
        """制作MD5"""
        hl_md5 = hashlib.md5()
        hl_md5.update(data.encode("utf-8"))
        return hl_md5.hexdigest()

    @staticmethod
    def make_token() -> str:
        """生成唯一Token"""
        ms = int(time.time() * 1000)
        token = ToolsUtil.make_md5(
            f"{ToolsUtil.make_uuid()}{ms}{ToolsUtil.random_string(8)}"
        )
        token_secret = f"{token}{ToolsUtil.secret}"
        return f"{ToolsUtil.make_md5(token_secret)}{ToolsUtil.random_string(6)}"

    @staticmethod
    def json_to_map(json_str: str) -> dict:
        """JSON转dict"""
        return json.loads(json_str)

    @staticmethod
    def to_lower_camel(string: str) -> str:
        """
        将连字符串转换为驼峰式字符串 首字符小写 to_camel->toCamel
        """
        words = string.split("_")
        if not words:
            return ""
        # 首单词小写
        first_word = words[0].lower()
        # 后续单词首字母大写
        remaining_words = [word.capitalize() for word in words[1:]]
        # 拼接结果
        return first_word + "".join(remaining_words)

    @staticmethod
    def safe_iterable(list: list[str] | None) -> Iterable[str]:
        """确保输入总是可迭代的，即使它为None"""
        return list if list is not None else []

    @staticmethod
    def str_to_list(data: str) -> list[str]:
        """将字符串转换为列表"""
        if "," in data:
            return data.split(",")
        else:
            return [data]

    @staticmethod
    def list_to_str(list: list[str]) -> str:
        """将列表转换为字符串"""
        return ",".join(list)

    @staticmethod
    def set_to_list(set: Union[set, None]) -> List[str]:
        """将集合转换为列表"""
        return list(set) if set is not None else []

    @staticmethod
    def set_to_str( set: Union[set, None]) -> str:
        """将集合转换为字符串"""
        return ToolsUtil.list_to_str(ToolsUtil.set_to_list(set))

    @staticmethod
    def str_to_set(srt: str) -> set:
        """将字符串转换为集合"""
        return set(ToolsUtil.str_to_list(srt))
    @staticmethod
    def generate_string(length: int = 8) -> str:
        """
        生成随机字符串
        :param length: 字符串长度
        """
        return "".join(random.sample(string.ascii_letters + string.digits, length))
    @staticmethod
    def has_duplicates(list1, list2):
        set1 = set(list1)
        set2 = set(list2)
        return not set1.isdisjoint(set2)
